package cn.wolfcode.wolf2w.redis.impl;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.redis.IRedisStatisService;
import cn.wolfcode.wolf2w.redis.util.RedisKeys;
import cn.wolfcode.wolf2w.repository.StrategyRepository;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.util.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.concurrent.TimeUnit;

@Service
public class RedisStatisServiceImpl implements IRedisStatisService {

    @Autowired
    private StringRedisTemplate template;
    @Autowired
    private IStrategyService strategyService;

    /**
     * 阅读操作
     * @param sid
     * @param num
     */
    @Override
    public void addViewnum(String sid,int num) {
        /*String key = RedisKeys.STRATEGY_STATIS_VO.join(sid);
        StrategyStatisVO statisVo = this.getStatisVo(sid);
        statisVo.setViewnum(statisVo.getViewnum()+ num);
        //更新vo对象
        template.opsForValue().set(key,JSON.toJSONString(statisVo));*/
        StrategyStatisVO vo = this.getStatisVo(sid);
        vo.setViewnum(vo.getViewnum()+num);
        this.setStatisVo(vo);
    }

    /**
     * 回复操作
     * @param sid
     * @param num
     */
    @Override
    public void addReplynum(String sid, int num) {
        /*//拼接redis中的key
        String key = RedisKeys.STRATEGY_STATIS_VO.join(strategyId);
        //获取vo对象
        StrategyStatisVO vo = this.getStatisVo(strategyId);
        //回复数+1
        vo.setReplynum(vo.getReplynum()+num);
        //更新vo
        template.opsForValue().set(key, JSON.toJSONString(vo));*/
        //抽取之后的操作
        //获取vo对象
        StrategyStatisVO vo = this.getStatisVo(sid);
        vo.setReplynum(vo.getReplynum()+num);
        this.setStatisVo(vo);
    }
    /**
     * 收藏操作(代码优化)
     * @param sid
     * @param uid
     * @return
     */
    @Override
    public Boolean favor(String sid, String uid) {
        //拼接收藏id的key
        String key = RedisKeys.STRATEGY_FAVOR_VO.join(uid);
        //初始化,sid的list
        List<String> sidList = new ArrayList<>();
        if(template.hasKey(key)){
            String s = template.opsForValue().get(key);
            sidList = JSON.parseArray(s, String.class);
        }
        boolean flag = false;
        //h获取vo对象
        StrategyStatisVO vo = this.getStatisVo(sid);
        if(sidList.contains(sid)){
            vo.setFavornum(vo.getFavornum()-1);
            sidList.remove(sid);
        }else{
            vo.setFavornum(vo.getFavornum()+1);
            sidList.add(sid);
            flag = true;
        }
        template.opsForValue().set(key,JSON.toJSONString(sidList));
        this.setStatisVo(vo);

        return flag;
    }
   /* @Override
    public Boolean favor(String sid, String uid) {
        StrategyStatisVO vo = this.getStatisVo(sid);
        List<String> list = new ArrayList<>();
        String key = RedisKeys.STRATEGY_FAVOR_VO.join(uid);
        if(template.hasKey(key)){
            String st = template.opsForValue().get(key);
            list = JSON.parseArray(st,String.class);
            if(list.contains(sid)){
                //说明已经收藏;收藏数减一
                vo.setFavornum(vo.getFavornum()-1);
                this.setStatisVo(vo);
                list.remove(sid);
                template.opsForValue().set(key,JSON.toJSONString(list));
                return false;
            }else{
                //说明没有收藏;收藏数加一
                vo.setFavornum(vo.getFavornum()+1);
                this.setStatisVo(vo);
                list.add(sid);
                template.opsForValue().set(key,JSON.toJSONString(list));
                return true;
            }
        }else{
            list.add(sid);
            template.opsForValue().set(key,JSON.toJSONString(list));
        }
        return true;
    }*/


    @Override
    public List<String> strategiesFavor(String uid) {
        if(!StringUtils.hasLength(uid)){
            return Collections.EMPTY_LIST;
        }
        String key = RedisKeys.STRATEGY_FAVOR_VO.join(uid);
        List<String> sids = new ArrayList<>();
        if(template.hasKey(key)){
            String sidStr = template.opsForValue().get(key);
            sids = JSON.parseArray(sidStr, String.class);
        }
        return sids;
    }

    @Override
    public Boolean strategyThumbup(String sid, String uid) {
        String key = RedisKeys.STRATEGY_THUMBUP_VO.join(uid, sid);
        StrategyStatisVO vo = this.getStatisVo(sid);
        if(!template.hasKey(key)){
            //没有说明今天没顶过
            Date date = new Date();
            Date endDate = DateUtil.getEndDate(date);
            long time = DateUtil.getDateBetween(date, endDate);
            template.opsForValue().set(key,"1",time,TimeUnit.SECONDS);
            vo.setThumbsupnum(vo.getThumbsupnum()+1);
            this.setStatisVo(vo);
            return true;
        }
        return false;
    }

    @Override
    public boolean isExistVo(String sid) {
        String key = RedisKeys.STRATEGY_STATIS_VO.join(sid);
        if(template.hasKey(key)){
            return true;
        }
        return false;
    }

    @Override
    public List<StrategyStatisVO> getStatisVoList(String pattern) {
        //根据传来的*,拼接出   XXX:*
        String keyPattern = RedisKeys.STRATEGY_STATIS_VO.join(pattern);
        List<StrategyStatisVO> list = new ArrayList<>();
        //根据XXX:*找出所有的XXX开头的key的集合
        Set<String> keys = template.keys(keyPattern);
        //判断其不为空
        if( keys!=null && keys.size()>0){
            //遍历所有的key
            for (String key : keys) {
                //获取value
                String v = template.opsForValue().get(key);
                //将value转化为StrategyStatisVO对象再添加到list中返回
                list.add(JSON.parseObject(v,StrategyStatisVO.class));
            }
        }
        return list;
    }

    @Override
    public StrategyStatisVO getStatisVo(String sid) {
        String key = RedisKeys.STRATEGY_STATIS_VO.join(sid);
        StrategyStatisVO statisVO = null;
        //如果有值则说明存在对象
        if(template.hasKey(key)){
            String value = template.opsForValue().get(key);
            statisVO = JSON.parseObject(value, StrategyStatisVO.class);
        }else{
            //没有对象;
            //先初始化对象
            statisVO = new StrategyStatisVO();
            Strategy strategy = strategyService.get(sid);
            BeanUtils.copyProperties(strategy,statisVO);
            statisVO.setStrategyId(strategy.getId());
        }
        return statisVO;
    }

    @Override
    public void setStatisVo(StrategyStatisVO vo) {
        String key = RedisKeys.STRATEGY_STATIS_VO.join(vo.getStrategyId());
        template.opsForValue().set(key, JSON.toJSONString(vo));
    }


}
